package patterns.service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2016/12/12 0012.
 * 缓存类
 */
public class Cache {
    private List<IService> services;

    public Cache() {
        services = new ArrayList<>();
    }

    public List<IService> getServices() {
        return services;
    }

    public void addServices(IService service) {
        boolean exists = false;
        for (IService s : services) {
            if (s.getName().equalsIgnoreCase(service.getName())) {
                exists = true;
            }
        }
        if (!exists) {
            services.add(service);
        }
    }

    public IService getService(String serviceName) {
        for (IService s : services) {
            if (s.getName().equalsIgnoreCase(serviceName)) {
                return s;
            }
        }
        return null;
    }
}